package com.example.day9_sqlite2;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        userDao = new UserDao(this);
        findViewById(R.id.bt_insert).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
        findViewById(R.id.bt_select).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select();
            }
        });
        findViewById(R.id.bt_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });
        findViewById(R.id.bt_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
        findViewById(R.id.bt_insert_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert1();
            }});
        findViewById(R.id.bt_insert_two).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert2();
            }
        });
    }
    //使用事务一次性插入500条数据
    private void insert2() {

        ArrayList<User> users = new ArrayList<>();
        users.add(new User("nsdd","23556"));
        users.add(new User("nsdd","23556"));
        users.add(new User("nsdd","23556"));
        users.add(new User("nsdd","23556"));
        users.add(new User("nsdd","23556"));
        userDao.addAll(users);
    }
    //不使用事务一次性插入500条数据
    private void insert1() {

    }
    private void update() {
        userDao.update(new User(1,"张力文","234566"));
    }

    private void delete() {
        int i = userDao.deleteById(1);
        Toast.makeText(this, ""+i, Toast.LENGTH_SHORT).show();

    }

    private void select() {
        List<User> users = userDao.selectAll();
        Log.d("ytx", "select: "+users);
        User user = userDao.selectByName(new User("张文利", "123456"));
        Log.d("ytx", "select: "+user);
    }

    private void insert() {
        userDao.add(new User("姚甜雪1","123456"));
    }
}

